package com.jade.bss.admin.permission.impl;

import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.admin.AdminConstants;
import com.jade.bss.admin.AdminException;
import com.jade.bss.admin.permission.PermissionDao;
import com.jade.bss.admin.permission.PermissionEntry;
import com.jade.bss.admin.permission.PermissionGroup;
import com.jade.bss.admin.permission.PermissionManager;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用PermissionDao来进行权限数据访问。
 *
 * @author sky
 * @version 1.0 13-4-18 上午10:02
 */
@Named ("bss_permissionManager")
public class PermissionManagerImpl implements PermissionManager
{
    protected PermissionDao permissionDao;

    @Inject
    public void setPermissionDao(@Named ("bss_permissionDao") PermissionDao permissionDao)
    {
        this.permissionDao = permissionDao;
    }

    public PermissionEntry getPermission(int id)
    {
        return permissionDao.getPermission(id);
    }

    public PermissionEntry[] getPermission(int[] ids)
    {
        PermissionEntry[] entries = new PermissionEntry[ids.length];
        for (int i = 0; i < ids.length; i++) {
            entries[i] = getPermission(ids[i]);
        }
        return entries;
    }

    public List<PermissionEntry> listPermission()
    {
        return permissionDao.listPermission();
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public int addPermission(PermissionEntry permission)
            throws AdminException
    {
        return permissionDao.addPermission(permission);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public int addPermission(PermissionEntry permission, int groupId)
            throws AdminException
    {
        PermissionGroup group = permissionDao.getGroup(groupId);
        if (group == null) {
            throw new AdminException(AdminConstants.ERR_PERM_GRP_NOT_EXISTED, "permission group not existed", null,
                                     groupId);
        }
        permission.setGroupId(groupId);
        return permissionDao.addPermission(permission);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void associate(int permissionId, int groupId)
            throws AdminException
    {
        PermissionGroup group = permissionDao.getGroup(groupId);
        if (group == null) {
            throw new AdminException(AdminConstants.ERR_PERM_GRP_NOT_EXISTED, "permission group not existed", null,
                                     groupId);
        }
        PermissionEntry permission = permissionDao.getPermission(permissionId);
        if (permission == null) {
            throw new AdminException(AdminConstants.ERR_PERM_NOT_EXISTED, "permission not existed", null, permissionId);
        }

        permission.setGroupId(groupId);
        permissionDao.updatePermission(permission);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void associate(int[] permissionId, int groupId)
            throws AdminException
    {
        for (int pId : permissionId) {
            associate(pId, groupId);
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void dissociate(int permissionId)
            throws AdminException
    {
        PermissionEntry permission = permissionDao.getPermission(permissionId);
        if (permission == null) {
            throw new AdminException(AdminConstants.ERR_PERM_NOT_EXISTED, "permission not existed", null, permissionId);
        }
        permission.setGroupId(null);
        //		PermissionEntry  newPermission = new PermissionEntry();
        //		newPermission.setId(permission.getId());
        //		newPermission.setDisplayName(permission.getDisplayName());
        //		newPermission.setAction(permission.getAction());
        //		newPermission.setClassDisplayName(permission.getClassDisplayName());
        //		newPermission.setClassName(permission.getClassName());
        //		newPermission.setCreationDate(permission.getCreationDate());
        //		newPermission.setName(permission.getName());

        permissionDao.updatePermission(permission);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void dissociate(int[] permissionId)
            throws AdminException
    {
        for (int pId : permissionId) {
            dissociate(pId);
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void removePermission(int permissionId)
            throws AdminException
    {
        permissionDao.removePermission(permissionId);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void removePermission(int[] permissionIds)
            throws AdminException
    {
        for (int id : permissionIds) {
            removePermission(id);
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void removeGroup(int id)
            throws AdminException
    {
        permissionDao.removeGroup(id);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public int addGroup(PermissionGroup group)
            throws AdminException
    {
        return permissionDao.addGroup(group);
    }

    public List<PermissionGroup> listGroup()
    {
        return permissionDao.listGroup();
    }

    public PermissionGroup getGroup(int id)
    {
        return permissionDao.getGroup(id);
    }

    public PermissionEntry getPermission(String name)
    {
        return permissionDao.getPermission(name);
    }
}
